package com.hoapital.outpbillwork.module.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoapital.outpbillwork.module.entity.OutpBillItems;
import com.hoapital.outpbillwork.module.entity.OutpOrderDescTemp;
import com.hoapital.outpbillwork.module.mapper.OutpOrderDescTempMapper;
import com.hoapital.outpbillwork.module.service.OutpOrderDescTempService;
import com.hospital.common.core.constants.OrderConstant;
import com.hospital.common.core.exception.ExceptionHelpEnum;
import com.hospital.common.core.result.SuccessTip;
import com.hospital.common.core.result.Tip;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lvyao
 * @since 2021-02-25
 */
@Service
public class OutpOrderDescTempServiceImpl extends ServiceImpl<OutpOrderDescTempMapper, OutpOrderDescTemp> implements OutpOrderDescTempService {
    @Resource
    private OutpOrderDescTempMapper outpOrderDescTempMapper;

    @Override
    public OutpOrderDescTemp getOrderTempDate(String visitDate, String visitNo) {
        return outpOrderDescTempMapper.getOrderTempDate(visitDate, visitNo);
    }

    @Override
    public int deletTemp(String visitDate, String visitNo) {
        return outpOrderDescTempMapper.deletTemp(visitDate, visitNo);
    }

    @Override
    public Tip orderCheckStock(String orderNo) {
        Tip tip = new SuccessTip();
        int i = outpOrderDescTempMapper.queryCountTemp(orderNo);
        if (i==0){
            tip.setCode(ExceptionHelpEnum.BILL_OLD.getCode());
            tip.setMessage(ExceptionHelpEnum.BILL_OLD.getMessage());
            return tip;
        }
        int count = outpOrderDescTempMapper.isVerifyPresc(orderNo);
        if (count==0){
            tip.setCode(ExceptionHelpEnum.NOT_VERIFY_PRESC.getCode());
            tip.setMessage(ExceptionHelpEnum.NOT_VERIFY_PRESC.getMessage());
            return tip;
        }
        List<OutpBillItems> outpBillItems = outpOrderDescTempMapper.orderCheckStock(orderNo).stream().filter(outpBillItems1 -> outpBillItems1!=null).collect(Collectors.toList());
        if (outpBillItems!=null&&outpBillItems.size()>0){
            List<OutpBillItems> collect = outpBillItems.stream().filter(outpBillItems1 -> outpBillItems1.getNowBatchNo().equals(OrderConstant.NO_STOCK_FLAG)).collect(Collectors.toList());
            List<OutpBillItems> collect1 = collect.stream().filter(outpBillItems1 -> outpBillItems1 != null).collect(Collectors.toList());
            if (collect1!=null&&collect1.size()>0){
                tip.setCode(ExceptionHelpEnum.NO_STOCK.getCode());
                tip.setMessage(ExceptionHelpEnum.NO_STOCK.getMessage());
                tip.setData(collect1);
                return tip;
            }
        }
        return tip;
    }

    @Override
    public int isValidVisitDate(String visitDate, String visitNo) {
        return 0;
    }

    @Override
    public int getStockByExpCode(String expCode, String itemSpec, String performedBy) {
        return 0;
    }

}
